package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveQueryDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.model.common.vos.PageResultVo;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmNewsSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class WmNewsSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmNewsSensitiveService {
    /**
     * 删除敏感词
     *
     * @param id 敏感词ID
     */
    @Override
    public ResponseResult<String> delete(Integer id) {
        // 1. 参数校验
        if (id == null || id <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 2. 查询敏感词是否存在
        WmSensitive sensitive = lambdaQuery().eq(WmSensitive::getId, id).one();
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 3. 删除敏感词
        boolean isRemove = lambdaUpdate()
                .eq(WmSensitive::getId, id)
                .remove();
        return isRemove ? ResponseResult.okResult("删除成功") : ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 查询敏感词
     *
     * @param dto 查询参数
     */
    @Override
    public ResponseResult<PageResultVo<WmSensitive>> querySensitive(SensitiveQueryDto dto) {
        // 1. 检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam(); // 检查分页参数

        // 2. 分页查询
        Page<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        Page<WmSensitive> pageR = page(page);

        // 2.1 预构建结果集
        PageResultVo<WmSensitive> pageResultVo = new PageResultVo<>();

        // 3. 返回结果
        if (pageR == null || pageR.getRecords() == null) {

            return ResponseResult.okResult(pageResultVo); // 没有敏感词
        }

        pageResultVo.setRecords(pageR.getRecords());
        pageResultVo.setPage(pageR.getCurrent());
        pageResultVo.setSize(pageR.getSize());
        pageResultVo.setTotal(pageR.getTotal());

        return ResponseResult.okResult(pageResultVo);
    }

    /**
     * 新增敏感词
     *
     * @param wmSensitive 敏感词信息
     */
    @Override
    public ResponseResult<AppHttpCodeEnum> insert(WmSensitive wmSensitive) {
        // 1. 参数校验
        if (wmSensitive == null || wmSensitive.getSensitives() == null || StringUtil.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 2. 查询敏感词是否已存在
        WmSensitive sensitive = lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()).one();
        if (sensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词已存在");
        }

        // 3. 新增
        wmSensitive.setCreatedTime(new Date()); // 设置敏感词的创建时间为当前时间
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param wmSensitive 敏感词
     */
    @Override
    public ResponseResult<AppHttpCodeEnum> modify(WmSensitive wmSensitive) {
        // 1. 参数校验
        if (wmSensitive == null || wmSensitive.getId() == null || wmSensitive.getId() <= 0 ||
                StringUtil.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 2. 查询敏感词是否存在
        WmSensitive sensitive = lambdaQuery().eq(WmSensitive::getId, wmSensitive.getId()).one();
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不存在");
        }

        // 3. 修改
        wmSensitive.setCreatedTime(new Date()); // 设置敏感词的创建时间为当前时间
        updateById(wmSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
